package com.ningju.procurement.core.answer.service;

import com.ningju.procurement.common.service.BaseServiceImpl;
import com.ningju.procurement.core.answer.bean.Paper;
import com.ningju.procurement.core.answer.dao.PaperDao;
import com.ningju.procurement.core.questionnaire.bean.Topic;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 答卷
 */
@Service
public class PaperService extends BaseServiceImpl<PaperDao, Paper> {

    @Autowired
    private PaperDao paperDao;


    /**
     * 返回试卷的下一题
     * @param paper
     * @return
     */
    public Topic getNextTopic(Paper paper) {

        Integer paperAnswerSize = 1;
        List<Topic> topics = paper.getTestProject().getSourceProject().getTopicList();

        if(CollectionUtils.isNotEmpty(paper.getAnswerList())) {
            paperAnswerSize = paper.getAnswerList().size();
        }

        if(CollectionUtils.isNotEmpty(topics)) {
            return topics.get(paperAnswerSize - 1);
        }

        return null;
    }

    @Transactional
    public boolean save(Paper paper) {
        return super.save(paperDao, paper);
    }

}
